<?php
 /**
  * 导出部门和用户信息
  * **/
apf_require_class('Oa_Job_Attendance_Abstract');
class Oa_Job_Attendance_ExportDepartmentInfo extends Oa_Job_Attendance_Abstract {

    /** 数据库读 **/
    private $readdb = 'hroa_slave';//hroa_slave

    /** 数据库写 **/
    private $writedb = 'hroa_master';

    /** 默认CEO链接 **/
    private $ceo_id = "6932";
    private $cities = array();

    public function run() {
        ini_set("memory_limit",'512M');
        $start = microtime(true);
        $this->log("开始执行啦！");
        //$this->db_connect();

        $this->build_data();

        $this->log ( "开始..." );
        //导出金蝶里面坏的部门数据
        //$this->import_bad_department_info();
        $end = microtime(true) - $start;
        $this->log("共耗时:".$end);
        $this->log("job执行结束！");
    }

    private function build_data() {
        $this->cities = $this->_get_cities();
        $s_sql = 'SELECT b.relation_id,b.user_id,a.user_name,a.job_status,b.parent_id,b.user_job_id,c.department_id,d.department_name,e.bu_name,d.city_id FROM user AS a
                LEFT JOIN relation AS b ON a.user_id = b.user_id
                LEFT JOIN job AS c ON b.user_job_id = c.job_id
                LEFT JOIN department AS d ON c.department_id = d.department_id
                LEFT JOIN business_unit AS e ON d.bu_id = e.bu_id';

        $rs = $this->get_db_rows($s_sql, $this->readdb);
        if(empty($rs)) {return array();}

        $arr = array();
        foreach($rs as $v) {
            if($v['job_status'] != '1' || empty($v['user_id']) || empty($v['user_job_id'])) {continue;}
            $arr[$v['relation_id']]['user_id'] = $v['user_id'];
            $arr[$v['relation_id']]['user_name'] = $v['user_name'];
            $arr[$v['relation_id']]['parent_id'] = $v['parent_id'];
            $arr[$v['relation_id']]['user_job_id'] = $v['user_job_id'];
            $arr[$v['relation_id']]['department_name'] = $v['department_name'];
            $arr[$v['relation_id']]['bu_name'] = $v['bu_name'];
            $arr[$v['relation_id']]['city_name'] = $this->cities[$v['city_id']];
        }

         foreach($arr as $relation_id => $info) {
             $i_sql = "insert into export_department (relation_id,user_id,user_name,parent_id,user_job_id,department_name,bu_name,city_name) value
             ({$relation_id},{$info['user_id']},'{$info['user_name']}',{$info['parent_id']},{$info['user_job_id']},'{$info['department_name']}','{$info['bu_name']}','{$info['city_name']}')";
             $this->execute($i_sql,array(),$this->writedb);
         }

        //$s = $this->tree($arr,$this->ceo_id);
        //print_r($s);
    }



   public function tree($table,$p_id) {
        $tree = array();
        foreach($table as $id => $row){
            if($row['parent_id']==$p_id){
                $tmp = $this->tree($table,$row['user_id']);
                if($tmp){
                    $row['children']=$tmp;
                }else{
                    $row['leaf'] = true;
                }
                unset($table[$id]);
                $tree[]=$row;
            }
        }
        return $tree;
    }

    private function _get_cities() {
        $result = array();
        $s_sql = "select * from city";
        $rs = $this->get_db_rows($s_sql, $this->readdb);
        if(empty($rs)) {
            return $result;
        }

        foreach($rs as $v) {
            $result[$v['city_id']] = $v['city_name'];
            //$result[trim($v['city_name'])] = $v['city_id'];
        }
        return $result;
    }
}
